export const findNodeUpper = (ele, tag) => {
    if (ele.parentNode) {
      if (ele.parentNode.tagName === tag.toUpperCase()) {
        return ele.parentNode
      } else {
        return findNodeUpper(ele.parentNode, tag)
      }
    }
  }
  
  export const findNodeUpperByClasses = (ele, classes) => {
    let parentNode = ele.parentNode
    if (parentNode) {
      let classList = parentNode.classList
      if (classList && classes.every(className => classList.contains(className))) {
        return parentNode
      } else {
        return findNodeUpperByClasses(parentNode, classes)
      }
    }
  }
  
  export const findNodeDownward = (ele, tag) => {
    const tagName = tag.toUpperCase()
    if (ele.childNodes.length) {
      let i = -1
      let len = ele.childNodes.length
      while (++i < len) {
        let child = ele.childNodes[i]
        if (child.tagName === tagName) return child
        else return findNodeDownward(child, tag)
      }
    }
  }

  // scrollTop animation
export const scrollTop = (el, from = 0, to, duration = 500, endCallback) => {
    if (!window.requestAnimationFrame) {
      window.requestAnimationFrame = (
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function (callback) {
          return window.setTimeout(callback, 1000 / 60)
        }
      )
    }
    const difference = Math.abs(from - to)
    const step = Math.ceil(difference / duration * 50)
  
    const scroll = (start, end, step) => {
      if (start === end) {
        endCallback && endCallback()
        return
      }
  
      let d = (start + step > end) ? end : start + step
      if (start > end) {
        d = (start - step < end) ? end : start - step
      }
  
      if (el === window) {
        window.scrollTo(d, d)
      } else {
        el.scrollTop = d
      }
      window.requestAnimationFrame(() => scroll(d, end, step))
    }
    scroll(from, to, step)
  }